package com.edu.medicalpro.network;


import com.edu.medicalpro.intercepter.AddTokenInterceptor;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    private static volatile  RetrofitClient mInstance;

    private static final  String BaseUrl="http://123.56.248.164:8888";

    private Retrofit mRetrofit;

    private RetrofitClient(){

    }

    public static RetrofitClient getInstance(){
            if (mInstance ==null){
                synchronized (RetrofitClient.class){
                    if (mInstance == null){
                        mInstance = new RetrofitClient();
                    }
                }
            }
            return mInstance;
    }
    public <T> T getService(Class<T> tClass){
        return getRetrofit().create(tClass);
    }

    private synchronized Retrofit getRetrofit() {
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl)
                    .client(okHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        }
        return mRetrofit;
    }

    private OkHttpClient okHttpClient (){
        return new OkHttpClient.Builder().addInterceptor(new AddTokenInterceptor()).build();
    }
}
